package com.imti.recyclerviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.imti.recyclerviewdemo.MyAdapter.MyHolder;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyHolder>
{

    private Context context;

    private List<ContactModel> data;

    public MyAdapter(Context context)
    {
        this.context = context;
    }

    public void setData(List<ContactModel> data)
    {
        this.data = data;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.list_item_contact, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyHolder holder, int position)
    {
        holder.contactIcon.setImageResource(data.get(position).getContactIconResId());
        holder.contactName.setText(data.get(position).getContactName());
    }

    @Override
    public int getItemCount()
    {
        return null != data ? data.size() : 0;
    }

    class MyHolder extends ViewHolder
    {

        ImageView contactIcon;

        TextView contactName;

        public MyHolder(@NonNull View itemView)
        {
            super(itemView);

            contactIcon = itemView.findViewById(R.id.list_item_contact_icon);
            contactName = itemView.findViewById(R.id.list_item_contact_name);
        }
    }
}
